/*
Copyright 2010 Johan Maasing

   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at

       http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.
 */

/*
 * This file is influenced by and contains some lines of code from:
 * http://code.google.com/p/openintents/source/browse/trunk/NotePad/src/org/openintents/distribution/GetFromMarketDialog.java
 * The original source file is lacking an apache license header but the entire project is Apache licensed.
 */
package nu.zoom.android.gdata.project.client;

import android.app.AlertDialog;
import android.content.ActivityNotFoundException;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.util.Log;
import android.widget.Toast;

public class FallbackAboutDialog extends AlertDialog implements
		AlertDialog.OnClickListener {
	private static final String LOG_TAG = WKC.LOG_TAG;

	private final Context context;

	public FallbackAboutDialog(final Context context) {
		super(context);
		this.context = context;

		String applicationVersion = null;
		String applicationName = null;
		try {
			PackageInfo pi = context.getPackageManager().getPackageInfo(
					context.getPackageName(), 0);
			applicationVersion = pi.versionName;
			applicationName = context.getString(pi.applicationInfo.labelRes);
		} catch (PackageManager.NameNotFoundException e) {
			Log.e(LOG_TAG, "Package name not found", e);
		}

		StringBuilder message = new StringBuilder();
		if (applicationName != null) {
			setTitle(applicationName);
			message.append(applicationName).append(" - ");
		}
		if (applicationVersion != null) {
			message.append(applicationVersion).append("\n");
		}
		message.append(context.getText(R.string.about_oi_not_installed));

		setMessage(message);
		setButton(context.getText(R.string.about_oi_get_button), this);
		setIcon(R.drawable.ic_menu_info_details) ;
	}

	@Override
	public void onClick(DialogInterface dialog, int which) {
		Uri uriMarket = Uri.parse(this.context
				.getString(R.string.aboutapp_market_uri));

		Intent intentMarket = new Intent(Intent.ACTION_VIEW);
		intentMarket.setData(uriMarket);

		Uri uriDeveloper = Uri.parse(this.context
				.getString(R.string.aboutapp_developer_uri));
		Intent intentDeveloper = new Intent(Intent.ACTION_VIEW);
		intentDeveloper.setData(uriDeveloper);
		startSaveActivity(this.context, intentMarket, intentDeveloper);
	}

	/**
	 * Start an activity but prompt a toast if activity is not found (instead of
	 * crashing).
	 * 
	 * @param context
	 * @param intentMarket
	 * @param intentDeveloper
	 *            Alternative intent to call, if the first is not reachable
	 */
	public static void startSaveActivity(Context context, Intent intentMarket,
			Intent intentDeveloper) {
		try {
			context.startActivity(intentMarket);
		} catch (ActivityNotFoundException e) {
			Log.e(LOG_TAG,
					"Error getting OI About from market, will fallback on installing from developer.",
					e);
			try {
				context.startActivity(intentDeveloper);
			} catch (ActivityNotFoundException e2) {
				Log.e(LOG_TAG, "Error starting installing from developer.", e2);
				Toast.makeText(context, R.string.about_oi_update_error,
						Toast.LENGTH_SHORT).show();
			}
		}
	}
}
